import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import settingsRouter from './modules/settings'
import permissionsRouter from './modules/permissions'

// 动态路由规则在这里
export const asyncRoutes = [departmentsRouter, employeesRouter, settingsRouter, permissionsRouter]

// 静态路由规则在这里
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    // ! excel 导入要用到的组件
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'import',
        component: () => import('@/views/import/index')
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'shousi' }
      }
    ]
  }

  // 404 page must be placed at the end !!!
  // 这里和 动态路由 里面的 404 重复了，划掉这个
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),

    // 扩展运算符，合并静态路由和动态路由
    // routes: [...constantRoutes, ...asyncRoutes]

    // 把 动态路由 放到了 permission 里面，通过权限渲染
    // 这里只放 静态路由
    routes: [...constantRoutes]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// ! 清空路由的函数
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
